package top.xia17.plugins.fast.user.rest;

import top.xia17.plugins.fast.common.model.Back;
import top.xia17.plugins.fast.common.model.PageData;
import top.xia17.plugins.fast.common.model.PageParam;
import top.xia17.plugins.fast.common.database.jpa.JpaUtil;
import top.xia17.plugins.fast.common.utils.VerifyUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import top.xia17.plugins.fast.user.dao.entity.Dept;
import top.xia17.plugins.fast.user.model.dto.DeptDto;
import top.xia17.plugins.fast.user.model.query.DeptQuery;
import top.xia17.plugins.fast.user.service.DeptService;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;

/**
 * 部门rest
 *
 * @author xia17
 * @date 2022/1/17
 */
@RestController
@RequestMapping("dept/mgmt")
@RequiredArgsConstructor
public class DeptMgmtRest {

    private final DeptService deptService;


    /**
     * 新增部门
     *
     * @param resource /
     * @return /
     */
    @PostMapping("create")
    public Back<Dept> create(@RequestBody @Valid Dept resource) {
        Dept dept = deptService.create(resource);
        return Back.ok(dept);
    }


    /**
     * 修改部门
     *
     * @param resource /
     * @return /
     */
    @PostMapping("update")
    public Back<Dept> update(@RequestBody @Valid Dept resource) {
        Dept dept = deptService.update(resource);
        return Back.ok(dept);
    }

    /**
     * 删除部门
     *
     * @param id 部门id
     * @return /
     */
    @DeleteMapping
    public Back<String> delete(@NotBlank Long id) {
        VerifyUtil.isLongId(id, "参数异常");
        deptService.deleteById(id);
        return Back.ok();
    }

    /**
     * 删除多个部门
     *
     * @param ids 部门ids
     * @return /
     */
    @PostMapping("delete_by_ids")
    public Back<String> deleteByIds(@NotNull @RequestBody List<Long> ids) {
        deptService.deleteById(ids);
        return Back.ok();
    }


    /**
     * 部门分页列表
     *
     * @param deptQuery 查询条件
     * @param pageParam 分页参数
     * @return /
     */
    @GetMapping
    public Back<PageData<Dept>> page(DeptQuery deptQuery, @Valid PageParam pageParam) {
        Page<Dept> page = deptService.findAll(deptQuery, pageParam.toPageable());
        return Back.ok(JpaUtil.page(page));
    }


    /**
     * 部门列表
     *
     * @param deptQuery 查询条件
     * @return /
     */
    @GetMapping("list")
    public Back<List<Dept>> list(DeptQuery deptQuery) {
        List<Dept> list = deptService.findAll(deptQuery);
        return Back.ok(list);
    }


    /**
     * 获取部门的树结构
     * @return /
     */
    @GetMapping("get_dept_tree")
    public Back<List<DeptDto>> getDeptTree(){
        return Back.ok(deptService.getDeptTree());
    }


}
